import {Component, Input, OnInit, ViewChild} from "@angular/core";
import {Storage} from "@ionic/storage";
import _ from "lodash";
import {Common} from "../../../../providers/common";
import {IrpQjtHomeService} from "./home.service";
import {Content, LoadingController, Events,NavController} from 'ionic-angular';
import {IrpQjtHomeDetailPage} from "../detail/detail.component";

@Component({
  selector: "page-irp-panoramagram-home",
  templateUrl: "./home.html",
  providers: [IrpQjtHomeService]
})
export class IrpQjtHomePage {
  @ViewChild(Content) content: Content;
  showQueryCondition = true;
  managerName = null;
  allListDatas = [];
  startdate = null;
  enddate = null;
  orgid;
  allListDatasFlag = false;
  homeDetailPage=IrpQjtHomeDetailPage;
  // classifyTopList=[{name:"最新研究报告",value:"0"},{name:"最新股票研究报告排名",value:"1"},{name:"最近出差",value:"2"},
  //   {name:"最近策略推荐",value:"3"},{name:"最近金股推荐",value:"4"},{name:"最新晨会股票推荐",value:"5"},{name:"最新股票池调整",value:"6"}];
  // segmentClassifyTop="0";

  constructor(private IrpQjtHomeService: IrpQjtHomeService,
              private nav:NavController,
              private common: Common,
              private loadingCtrl: LoadingController,
              private events: Events) {
    let d=new Date();
    let m=d.getMonth()+1;
    this.enddate=d.getFullYear()+"-"+(m>9?m:"0"+m)+"-"+(d.getDate()>9?d.getDate():"0"+d.getDate());
    this.startdate=d.getFullYear()+"-01-01";
  }

  setShowQueryCondition() {
    this.showQueryCondition = !this.showQueryCondition;
    this.content.scrollToTop();
  }
  // initDataGridIonScroll() {
  //   let contentHeight = this.content.getContentDimensions().contentHeight;
  //   let height = 0;
  //   // height = contentHeight - document.getElementById("zjrqIonList").offsetHeight;
  //   // height = height - document.getElementById("classifyTopDiv").offsetHeight;
  //   if (document.getElementById("searchEle")) {
  //     height = height - document.getElementById("searchEle").offsetHeight;
  //   }
  //   height = height - document.getElementById("classifyTopDiv").offsetHeight;
  //   // let header = document.getElementsByTagName("ion-header")[0];
  //   // height = height - header["offsetHeight"];
  //   document.getElementById("DataGridIonScroll").style.height = height + "px";
  // }
  // classifyTopSegmentChange(){
  //
  // }
  search() {
    let loading = this.loadingCtrl.create({
      content: "加载中,请稍候...",
      // dismissOnPageChange: true
    });
    window['gfoa_loading'] = loading;
    loading.present();
    this.events.subscribe("loadingQJTDatas", () => {
      loading.dismiss();
      this.events.unsubscribe("loadingQJTDatas");
    });
    this.IrpQjtHomeService.getIRMSIRMOrgid(this.managerName).subscribe((data) => {
      if (data) {
        this.orgid = data[0];
        console.log(data);
        let qjtListDatas = [];
        let dataList, objData;
        this.IrpQjtHomeService.getIRMResearchReport(this.orgid, this.startdate, this.enddate).subscribe((data) => {
          dataList = [], objData = {
            fdata: [],
            data: [],
            title: ""
          };
          if (data) {
            _.forEach(data, (i) => {
              let d = i.split("|");
              dataList.push(d);
              if (objData.fdata.length < 5) objData.fdata.push(d);
            });
          }
          objData.data = dataList;
          objData.title = "最新研究报告";
          console.log(objData);
          qjtListDatas.push(objData);
          this.IrpQjtHomeService.getIRMResearchReportRank(this.orgid, this.startdate, this.enddate).subscribe((data) => {
            dataList = [], objData = {
              fdata: [],
              data: [],
              title: ""
            };
            if (data) {
              _.forEach(data, (i) => {
                let d = i.split("|");
                dataList.push(d);
                if (objData.fdata.length < 5) objData.fdata.push(d);
              });
            }
            objData.data = dataList;
            objData.title = "最新股票研究报告排名";
            console.log(objData);
            qjtListDatas.push(objData);
            this.IrpQjtHomeService.getIRMOnBusiness(this.orgid, this.startdate).subscribe((data) => {
              dataList = [], objData = {
                fdata: [],
                data: [],
                title: ""
              };
              if (data) {
                _.forEach(data, (i) => {
                  let d = i.split("|");
                  dataList.push(d);
                  if (objData.fdata.length < 5) objData.fdata.push(d);
                });
              }
              objData.data = dataList;
              objData.title = "最近出差";
              console.log(objData);
              qjtListDatas.push(objData);
              this.IrpQjtHomeService.getIRMPolicyRecommend(this.orgid, this.startdate, this.enddate).subscribe((data) => {
                dataList = [], objData = {
                  fdata: [],
                  data: [],
                  title: ""
                };
                if (data) {
                  _.forEach(data, (i) => {
                    let d = i.split("|");
                    dataList.push(d);
                    if (objData.fdata.length < 5) objData.fdata.push(d);
                  });
                }
                objData.data = dataList;
                objData.title = "最近策略推荐";
                console.log(objData);
                qjtListDatas.push(objData);
                this.IrpQjtHomeService.getIRMGoldStockRecommend(this.orgid, this.startdate, this.enddate).subscribe((data) => {
                  dataList = [], objData = {
                    fdata: [],
                    data: [],
                    title: ""
                  };
                  if (data) {
                    _.forEach(data, (i) => {
                      let d = i.split("|");
                      dataList.push(d);
                      if (objData.fdata.length < 5) objData.fdata.push(d);
                    });
                  }
                  objData.data = dataList;
                  objData.title = "最近金股推荐";
                  console.log(objData);
                  qjtListDatas.push(objData);
                  this.IrpQjtHomeService.getIRMMorningStorck(this.orgid, this.startdate, this.enddate).subscribe((data) => {
                    dataList = [], objData = {
                      fdata: [],
                      data: [],
                      title: ""
                    };
                    if (data) {
                      _.forEach(data, (i) => {
                        let d = i.split("|");
                        dataList.push(d);
                        if (objData.fdata.length < 5) objData.fdata.push(d);
                      });
                    }
                    objData.data = dataList;
                    objData.title = "最新晨会股票推荐";
                    console.log(objData);
                    qjtListDatas.push(objData);
                    this.IrpQjtHomeService.getIRMStockPoolAdjustment(this.orgid, this.startdate, this.enddate).subscribe((data) => {
                      dataList = [], objData = {
                        fdata: [],
                        data: [],
                        title: ""
                      };
                      if (data) {
                        _.forEach(data, (i) => {
                          let d = i.split("|");
                          dataList.push(d);
                          if (objData.fdata.length < 5) objData.fdata.push(d);
                        });
                      }
                      objData.data = dataList;
                      objData.title = "最新股票池调整";
                      console.log(objData);
                      qjtListDatas.push(objData);

                      this.allListDatas = qjtListDatas;
                      console.log(this.allListDatas);
                      this.showQueryCondition = !this.showQueryCondition;
                      if (this.allListDatas) {
                        this.allListDatasFlag = false;
                      } else {
                        this.allListDatasFlag = true;
                      }
                      loading.dismiss();
                      this.events.unsubscribe("loadingQJTDatas");
                    });
                  });
                });
              });
            });
          });
        });
      } else {
        this.allListDatasFlag = true;
        this.allListDatas = [];
        loading.dismiss();
        this.events.unsubscribe("loadingQJTDatas");
      }
    });
  }

  goDetail(item,index){
    this.nav.push(this.homeDetailPage,{
      item:item,
      index:index
    });
  }

}
